import {
  getDueTasks,
} from "./dates.js";
import {
  setBarText,
} from "./common.js";

today();

export async function today(){
  /**
   * @since 2025-09-26 设置网页标题，按钮
   */
  document.title = chrome.i18n.getMessage("extension_name");
  $(".title").html(chrome.i18n.getMessage("today_title"));
  $(".close-btn").html(chrome.i18n.getMessage("close_btn"));

  /**
   * @since 2025-09-26 增加监听
   */
  $(".close-btn").on('click', function (){
    setBarText("");
    window.close();
  });

  /**
   * @since 2025-09-26 获取今日到期任务
   */
  let dueTasks = await getDueTasks();
  if(dueTasks.length == 0){
    window.close();
    return;
  } else {}

  /**
   * @since 2025-09-26 今日到期的任务列表
   */
  let htmlArray = new Array();
  for(let i=0; i<dueTasks.length; i++){
    htmlArray.push('<div class="task-item"><span class="task-number">'+ (i+1) +'</span><span class="task-text">'+ dueTasks[i]["title"] +'</span></div>');
  }
  $(".tasks-list").html(htmlArray.join(''))
}